package com.zzh.myapplication.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.zzh.myapplication.R;

/**
 * @author zzh
 * @Name:
 * @Description: ${todo}
 * @time 11:16
 * @copyright
 */
public class NoticeLinearLayout extends LinearLayout {
    private View _rootView;
    private Context _context;
    private MarqueeTextView _marquee_text_view;
    private int noticeState;   //0x01  启动播放动画状态; 0x02 滚动状态;0x04  消失动画状态; 0x00  未显示状态;
    private boolean isHasNewCome = false;

    public boolean isHasNewCome() {
        return isHasNewCome;
    }

    public void setHasNewCome(boolean hasNewCome) {
        isHasNewCome = hasNewCome;
    }

    private int ANIMATION_TIME = 1500;//动画时间

    public NoticeLinearLayout(Context context) {
        super(context);
        init(context);
    }

    public NoticeLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public NoticeLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        _context = context;
        _rootView = inflate(_context, R.layout.leba_notice_linear_layout, this);
        _marquee_text_view = (MarqueeTextView) _rootView.findViewById(R.id.notice_marquee_text_view);
    }

    public MarqueeTextView get_marquee_text_view() {
        return _marquee_text_view;
    }

    public void setText(String text) {
        _marquee_text_view.set_text(text);
    }

    //显示的动画
    public void startAnimation(final Activity parent_context) {
        Display display = parent_context.getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        this.measure(size.x, size.y);
        animHeightToView(this, 0, this.getMeasuredHeight(), true, ANIMATION_TIME);
    }

    //隐藏的动画
    public void stopAnimation() {
        animHeightToView(this, this.getMeasuredHeight(), 0, false, ANIMATION_TIME);
    }

    /**
     * 回调监听;
     */
    //显示或隐藏动画的监听
    private OnAnimationListner _animation_listner;

    public int getNoticeState() {
        return noticeState;
    }

    public void setNoticeState(int noticeState) {
        this.noticeState = noticeState;
    }

    public interface OnAnimationListner {
        //flag == false,表明view 不可见
        //flag == true,表明view 可见
        void onAnimationEndListner(boolean flag);
    }

    //字幕滚动完成的监听
    public void set_text_compelete_listner(MarqueeTextView.CompleteListener listner) {
        _marquee_text_view.set_listner(listner);
    }

    public void setOnAnimationEndListner(OnAnimationListner marqueeCompleteListener) {
        this._animation_listner = marqueeCompleteListener;
    }

    // 高度渐变的动画；
    private void animHeightToView(final View v, final int start, final int end, final boolean isToShow,
                                  long duration) {
        ValueAnimator va = ValueAnimator.ofInt(start, end);
        final ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int h = (int) animation.getAnimatedValue();
                layoutParams.height = h;
                v.setLayoutParams(layoutParams);
                v.requestLayout();
            }
        });

        va.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                if (isToShow) {
                    v.setVisibility(View.VISIBLE);
                }
                super.onAnimationStart(animation);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                if (!isToShow) {
                    v.setVisibility(View.GONE);
                }
                if (_animation_listner != null) {
                    _animation_listner.onAnimationEndListner(isToShow);
                }
            }
        });
        va.setDuration(duration);
        va.start();
    }
}
